﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using OgonPhonesXML.Cisco.Serializer;
using System.IO;
using System.Xml;

namespace OgonPhonesXML.Cisco
{
    class BaseCiscoPhone
    {
        protected XmlSerializer Serializer = null;
        protected Object Component = new Object();

        private CiscoIPPhoneMenuType ipphonemenu = null;
        public CiscoIPPhoneMenuType IPPhoneMenu
        {
            get
            {
                if (ipphonemenu == null)
                {

                    ipphonemenu = new CiscoIPPhoneMenuType();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneMenuType));
                    Component = ipphonemenu;
                }
                return ipphonemenu;
            }
        }

        private CiscoIPPhoneTextType ipphonetext = null;
        public CiscoIPPhoneTextType IPPhoneText
        {
            get
            {
                if (ipphonetext == null)
                {

                    ipphonetext = new CiscoIPPhoneTextType();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneTextType));
                    Component = ipphonetext;
                }
                return ipphonetext;
            }
        }

        private CicsoIPPhoneInputType ipphoneinput = null;
        public CicsoIPPhoneInputType IPPhoneInput
        {
            get
            {
                if (ipphoneinput == null)
                {

                    ipphoneinput = new CicsoIPPhoneInputType();
                    Serializer = new XmlSerializer(typeof(CicsoIPPhoneInputType));
                    Component = ipphoneinput;
                }
                return ipphoneinput;
            }
        }

        private CiscoIPPhoneDirectoryType ipphonedirectory = null;
        public CiscoIPPhoneDirectoryType IPPhoneDirecotory
        {
            get
            {
                if (ipphonedirectory == null)
                {

                    ipphonedirectory = new CiscoIPPhoneDirectoryType();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneDirectoryType));
                    Component = ipphonedirectory;
                }
                return ipphonedirectory;
            }
        }

        private CiscoIPPhoneIconMenuType ipphoneiconmenu = null;
        public CiscoIPPhoneIconMenuType IPPhoneIconMenu
        {
            get
            {
                if (ipphoneiconmenu == null)
                {

                    ipphoneiconmenu = new CiscoIPPhoneIconMenuType();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneIconMenuType));
                    Component = ipphoneiconmenu;
                }
                return ipphoneiconmenu;
            }
        }

        private CiscoIPPhoneExecuteType ipphoneexecute = null;
        public CiscoIPPhoneExecuteType IPPhoneExecute
        {
            get
            {
                if (ipphoneexecute == null)
                {

                    ipphoneexecute = new CiscoIPPhoneExecuteType();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneExecuteType));
                    Component = ipphoneexecute;
                }
                return ipphoneexecute;
            }
        }
        private CiscoIPPhoneResponseItemType ipphoneresponse = null;
        public CiscoIPPhoneResponseItemType IPPhoneResponse
        {
            get
            {
                if (ipphoneresponse == null)
                {

                    ipphoneresponse = new CiscoIPPhoneResponseItemType();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneResponseItemType));
                    Component = ipphoneresponse;
                }
                return ipphoneresponse;
            }
        }

        private CiscoIPPhoneError ipphoneerror = null;
        public CiscoIPPhoneError IPPhoneError
        {
            get
            {
                if (ipphoneerror == null)
                {

                    ipphoneerror = new CiscoIPPhoneError();
                    Serializer = new XmlSerializer(typeof(CiscoIPPhoneError));
                    Component = ipphoneerror;
                }
                return ipphoneerror;
            }
        }
        public String getXML()
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.GetEncoding("ISO-8859-1"));
            Serializer.Serialize(xmlTextWriter, this.Component, ns);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
        }
        public String getXML(bool status)
        {
            if (status == false)
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.OmitXmlDeclaration = true;
                StringWriter stringWriter = new StringWriter();
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
                {
                    Serializer.Serialize(xmlWriter, Component, ns);
                }
                string xmlText = stringWriter.ToString();
                return xmlText;
            }
            else
            {
                return getXML();
            }
        }
        private String UTF8ByteArrayToString(Byte[] characters)
        {

            UTF8Encoding encoding = new UTF8Encoding();

            String constructedString = encoding.GetString(characters);

            return (constructedString);

        }
    }
}
